#include <iostream>
#include <iterator>
#include <vector>
#include <list>
#include <algorithm>
#include <fstream>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "CShellProcess.h"
#include "thread/CThread.h"

using namespace std;

class ThreadTest : public CThread
{
public:
	ThreadTest() {};
	void run() {
		string cmd = "ps -ef";
		CShellProcess *proc = new CShellProcess(cmd, 1);
cout << "start" << endl;
		proc->start();
cout << "wait" << endl;
		proc->waits();
cout << "finish" << endl;
		delete proc;
	};
};

int main(int argc,char *argv[])
{
	if(argc<2) {
		cerr << "Usages : " << argv[0] << " -e <cmd1> [-e <cmd2>]" << endl;
		exit(-1);
	}

	vector<CShellProcess *> pv;

	int opt;
	static char optstr[] = "e";
	int cmd_pos = -1;

	while((opt=getopt(argc,argv,optstr))!=-1) {
		switch(opt) {
			case 'e' :
			{
				string cmd = argv[optind++];
				//CShellProcess *proc = new CShellProcess(cmd, 1);
				CShellProcess *proc = new CShellProcess(cmd);
				pv.push_back(proc);
				break;
			}
			default :
				cerr << "Usages : " << argv[0] << " -e <cmd1> [-e <cmd2>]" << endl;
				exit(-1);
		}
	}

	for(int i=0;i<pv.size();i++) {
		pv[i]->start();
	}

	for(int i=0;i<pv.size();i++) {
		pv[i]->waits();
		delete pv[i];
	}

/*
	cout << "thread test : " << getpid() << endl;
	ThreadTest thr_test;
	thr_test.start();
	thr_test.join();
	cout << "success" << endl;
*/
}
